﻿//using System;
//using System.Collections.Generic;
//using System.Text;
//using System.Drawing;
//using Microsoft.DirectX;
//using Microsoft.DirectX.Direct3D;


//namespace MulticastTV
//{
//    public partial class d3d
//    {
//        Form1 f;
        
//        // Managed Direct3D Device
//        private Device device = null;
//        private PresentParameters presentParams = null;

//        // Managed Direct3D Resources
//        private Surface renderTarget;
//        private VertexBuffer vertexBuffer = null;
//        private CustomVertex.TransformedColoredTextured[] vertices = new CustomVertex.TransformedColoredTextured[8];

//        // The window  size and backbuffer default size
//        private Size displaySize = new Size(1024, 768);

//        // Lost device
//        bool deviceLost = false;


//        //Constructor
//        public d3d (Form1 form)
//        {
//            f = form;
//            displaySize = f.pictureBox1.Size;
//        }

//        //Destructor
//        ~d3d()
//        {
//            Dispose();
//        }

//        // Init the Managed Direct3D Device
//        public void InitializeGraphics()
//        {
//            presentParams = new PresentParameters();

//            // This sample is only windowed
//            presentParams.Windowed = true;

//            // Enable Z-Buffer
//            // This is not really needed in this sample but real applications generaly use it
//            presentParams.EnableAutoDepthStencil = true;
//            presentParams.AutoDepthStencilFormat = DepthFormat.D16;

//            // Hint to the Device Driver : This is a video playing application
//            presentParams.PresentFlag = PresentFlag.Video;

//            // How to swap backbuffer in front and how many frames per screen refresh
//            presentParams.SwapEffect = SwapEffect.Discard;
//            presentParams.PresentationInterval = PresentInterval.One;

//            // Disable Managed Direct3D events
//            Device.IsUsingEventHandlers = false;

//            // Create the device
//            device = new Device(
//              Manager.Adapters.Default.Adapter,
//              DeviceType.Hardware,
//              f.pictureBox1,
//              CreateFlags.SoftwareVertexProcessing | CreateFlags.MultiThreaded,
//              presentParams
//              );

//            AllocateResources();
//            InitializeDevice();
//        }

//        private void resetBackBufferSize()
//        {
//            presentParams.BackBufferWidth = f.pictureBox1.Width;
//            presentParams.BackBufferHeight = f.pictureBox1.Height;
            
//            //TODO: pause render loop 
//            device.Reset(presentParams);
//        }

//        private void AllocateResources()
//        {
//            // Retrieve the render tatget (the backbuffer)
//            renderTarget = device.GetRenderTarget(0);

//            // Alloc a Vertex buffer to draw the video (4 vertices -> a Quad)
//            vertexBuffer = new VertexBuffer(
//              typeof(CustomVertex.TransformedColoredTextured),
//              vertices.Length,
//              device,
//              Usage.Dynamic | Usage.WriteOnly,
//              CustomVertex.TransformedColoredTextured.Format,
//              Pool.Default
//              );
//        }

//        private void InitializeDevice()
//        {
//            FilterCaps filterCaps = device.DeviceCaps.TextureFilterCaps;

//            // If the hardware support it, apply a bilinear filtering
//            if (filterCaps.SupportsMagnifyLinear && filterCaps.SupportsMinifyLinear)
//            {
//                device.SamplerState[0].MinFilter = TextureFilter.Linear;
//                device.SamplerState[0].MagFilter = TextureFilter.Linear;
//            }
//            else
//            {
//                device.SamplerState[0].MinFilter = TextureFilter.Point;
//                device.SamplerState[0].MagFilter = TextureFilter.Point;
//            }

//            device.RenderState.AlphaBlendEnable = true;
//            device.RenderState.SourceBlend = Blend.SourceAlpha;
//            device.RenderState.DestinationBlend = Blend.InvSourceAlpha;
//            device.RenderState.AlphaTestEnable = true;
//            device.RenderState.ReferenceAlpha = 0x10;
//            device.RenderState.AlphaFunction = Compare.Greater;


//            initVerices();
//        }

//        private void initVerices()
//        {
//            //
//            for (int i = 0; i < vertices.Length; i++)
//            {
//                vertices[i].Rhw = 1;
//            }
//        }

//        public void Dispose()
//        {
//            shutdownDs();
//            ShutdownD3d();       
//        }

//        private void ShutdownD3d()
//        {
//            deviceLost = true;
            
//            // Free Managed Direct3D resources
//            renderTarget.Dispose();
//            vertexBuffer.Dispose();

//            // Free the Managed Direct3D device
//            device.Dispose();
//        }

//        public void UpdateEnvironment()
//        {
//            if (deviceLost)
//                return;

           
//            Size vmrTexSize = allocator.TextureSize;
//            Size vmrVidSize = allocator.VideoSize;
//            RectangleF videoClientRectangle = Rectangle.Empty;

//            if (vmrVidSize.Width >= vmrVidSize.Height)
//            {
//                // Compute the video aspect-ratio for a landscape proportioned image
//                float videoAR = (float)vmrVidSize.Height / (float)vmrVidSize.Width;

//                videoClientRectangle.X = 0.0f;
//                videoClientRectangle.Width = (float)displaySize.Width;
//                videoClientRectangle.Height = (float)displaySize.Width * videoAR;
//                videoClientRectangle.Y = ((float)displaySize.Height - videoClientRectangle.Height) / 2;
//            }
//            else
//            {
//                // Compute the video aspect-ratio for a portrait proportioned image
//                float videoAR = (float)vmrVidSize.Width / (float)vmrVidSize.Height;

//                videoClientRectangle.Y = 0.0f;
//                videoClientRectangle.Width = (float)displaySize.Height * videoAR;
//                videoClientRectangle.Height = (float)displaySize.Height;
//                videoClientRectangle.X = ((float)displaySize.Width - videoClientRectangle.Width) / 2;
//            }

//            // The Quad is built using a triangle fan of 2 triangles : 0,1,2 and 0, 2, 3
//            // 0 *-------------------* 1
//            //   |\                  |
//            //   |   \               |
//            //   |      \            |
//            //   |         \         |
//            //   |            \      |
//            //   |               \   |
//            //   |                  \|
//            // 3 *-------------------* 2

//            vertices[0].X = videoClientRectangle.X;
//            vertices[0].Y = videoClientRectangle.Y;
//            vertices[1].X = videoClientRectangle.Width;
//            vertices[1].Y = videoClientRectangle.Y;
//            vertices[2].X = videoClientRectangle.Width;
//            vertices[2].Y = videoClientRectangle.Height + videoClientRectangle.Y;
//            vertices[3].X = videoClientRectangle.X;
//            vertices[3].Y = videoClientRectangle.Height + videoClientRectangle.Y;

//            // See the allocator source to see why the texture size is not necessary the video size
//            vertices[0].Tu = 0.0f;
//            vertices[0].Tv = 0.0f;
//            vertices[1].Tu = (float)vmrVidSize.Width / (float)vmrTexSize.Width;
//            vertices[1].Tv = 0.0f;
//            vertices[2].Tu = (float)vmrVidSize.Width / (float)vmrTexSize.Width;
//            vertices[2].Tv = (float)vmrVidSize.Height / (float)vmrTexSize.Height;
//            vertices[3].Tu = 0.0f;
//            vertices[3].Tv = (float)vmrVidSize.Height / (float)vmrTexSize.Height;

//            vertices[0].Color = unchecked((int)0xffffffff); // low left
//            vertices[1].Color = unchecked((int)0xffffffff); // high left
//            vertices[2].Color = unchecked((int)0xffffffff); // low right
//            vertices[3].Color = unchecked((int)0xffffffff); // high right

//            //vertices[0].Z = 1.0f;
//            //vertices[1].Z = 1.0f;
//            //vertices[2].Z = 1.0f;
//            //vertices[3].Z = 1.0f;

//            updateOsd();

//            // Fill the vertex buffer
//            vertexBuffer.SetData(vertices, 0, LockFlags.Discard);

//        }

//        public void RenderEnvironment()
//        {
//            if (deviceLost)
//                return;
            
//            // Set the Render Target (the VMR9 change it)
//            device.SetRenderTarget(0, renderTarget);
//            // Clear it
//            device.Clear(ClearFlags.Target , Color.BlueViolet, 1.0f, 0);

//            device.BeginScene();

//            // Attach the vertex buffer to the Direct3D Device
//            device.SetStreamSource(0, vertexBuffer, 0);
//            device.VertexFormat = CustomVertex.TransformedColoredTextured.Format;

//            // Set the allocator texture as active texture
//            device.SetTexture(0, allocator.Texture);

//            // Draw the Quad
//            device.DrawPrimitives(PrimitiveType.TriangleFan, 0, 2);
           
//            drawOsd();

//            device.EndScene();

//            // Show what we draw
//            try
//            {
//                device.Present();
//            }

//            catch 
//            {
//                deviceLost = true;
//            }

//        }

//    }
//}
